package apiTest;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class TestTransaction {
    private static List<Transaction> transactions = null;
    static{
        Trader raoul = new Trader("Raoul","Cambridge");
        Trader mario = new Trader("Mario","Milan");
        Trader alan = new Trader("Alan","Cambridge");
        Trader brian = new Trader("Brian","Cambridge");

        transactions = Arrays.asList(
                new Transaction(brian,2011,300),
                new Transaction(raoul,2012,1000),
                new Transaction(raoul,2011,400),
                new Transaction(mario,2012,710),
                new Transaction(mario,2012,700),
                new Transaction(alan,2012,950)
                );
    }

    public static void main(String[] args) {
        //sort(transactions);
        //sortby2011(transactions);
        //city(transactions);
        //formCambridge(transactions);
       // workMilan(transactions);
        //countCambridgeValue(transactions);
       // maxValue(transactions);
        minValue(transactions);
    }

    //根据交易金额倒序输出所有交易信息
    private static void sort(List<Transaction> list){
        List<Transaction> collect = list.stream()
                .sorted((a, b) -> (b.getValue() - a.getValue()))
                .collect(Collectors.toList());
        collect.stream().forEach(System.out::println);
    }

    //找出2011年发生的所有交易， 并按交易额排序（从低到高）
    private static void sortby2011(List<Transaction> list){
        List<Transaction> collect = list.stream()
                .filter(transaction -> transaction.getYear() == 2011)
                .sorted((a, b) -> (a.getValue() - b.getValue()))
                .collect(Collectors.toList());
        collect.stream().forEach(System.out::println);
    }

    //交易员都在哪些不同的城市工作过？
    private static void city(List<Transaction> list){
        list.stream()
                .map(i->i.getTrader().getCity())
                .distinct()
                .forEach(System.out::println);
    }

    //查找所有来自剑桥的交易员，并按姓名排序
    private static void formCambridge(List<Transaction> list){
        list.stream()
                .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
                .sorted((a,b)->(a.getTrader().getName().compareTo(b.getTrader().getName())))
                .forEach(transaction ->{
                    Trader trader = transaction.getTrader();
                    System.out.println(trader);
                });
    }

    //有没有交易员是在米兰工作的？
    private static void workMilan(List<Transaction> list){
        boolean milan = list.stream()
                .anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan"));
        System.out.println(milan);
    }

    private static void countCambridgeValue(List<Transaction> list){
        int milan = list.stream()
                .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
                .mapToInt(Transaction::getValue)
                .sum();
        System.out.println(milan);
    }

    private static void maxValue(List<Transaction> list){
        int asInt = list.stream()
                .mapToInt(Transaction::getValue)
                .max()
                .getAsInt();
        System.out.println(asInt);
    }

    private static void minValue(List<Transaction> list){
        Optional<Transaction> min = list.stream()
                .min(Comparator.comparingInt(Transaction::getValue));
        System.out.println(min);
    }

}
